
#pragma once
#include "miniprotobuf_hlp.h"

class CMiniPb_person
{
public:


	CMiniPb_person(void)
	{

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));

	}


	CMiniPb_person(CMiniPb_person& r)
	{
		CMiniPb_person * pr = const_cast<CMiniPb_person *>(&r);

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));


		if (pr->has_age())
		{
			set_age(pr->age());
		}

		if (pr->size_of_skill()>0)
		{
			for (unsigned int i=0; i<pr->size_of_skill(); i++)
			{
				add_skill(pr->skill(i));
			}
		}

		if (pr->has_name())
		{
			set_name(pr->name());
		}

	}


	CMiniPb_person& operator =(CMiniPb_person& r)
	{
		CMiniPb_person * pr = const_cast<CMiniPb_person *>(&r);

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));


		if (pr->has_age())
		{
			set_age(pr->age());
		}

		if (pr->size_of_skill()>0)
		{
			for (unsigned int i=0; i<pr->size_of_skill(); i++)
			{
				add_skill(pr->skill(i));
			}
		}

		if (pr->has_name())
		{
			set_name(pr->name());
		}

		return *this;
	}


	~CMiniPb_person(void)
	{

		if (pFmt != NULL)
		{
			delete pFmt;
		}

	}


public:
	//format
	CFormat* pFmt;
	//idx
	//mask
#define MASK_SIZE 1
	unsigned int _mask[MASK_SIZE];
	SET_BIT_HELP_EASY(_mask,MASK_SIZE);
	HAS_BIT_HELP_EASY(_mask,MASK_SIZE);

	//attr enum

	enum {

		e_age = 3,

		e_skill = 4,

		e_name = 2,

	};

	//attr

	OPTION_ATTR(int, age);

	OPTION_ATTR(string, name);

	REPEATED_ATTR(string, skill);



	bool parse(const char* pIn, unsigned int packetlen)
	{
		if (pIn == NULL || packetlen == 0)
		{
			return false;
		}
		CBytebuffer bb((char*)pIn, packetlen, pFmt);
		return parse(&bb);
	}

	bool parse(CBytebuffer* pbb)
	{
		if (pbb == NULL)
		{
			return false;
		}
		int tag = 0;
		int datatype = 0;
		while ((tag = pbb->get_tag_datatype(datatype))>0)
		{
			switch(tag)
			{

			case e_age:
				{
					if ( !pbb->parse_varint32(_age))
					{
						return false;
					}
					_set_bit(tag);
				}
				break;

			case e_name:
				{
					if ( !pbb->parse_string(_name))
					{
						return false;
					}
					_set_bit(tag);
				}
				break;

			case e_skill:
				{
					int size = 0;
					if ( !pbb->parse_varint32(size))
					{
						return false;
					}
					int count = 0;
					if ( !pbb->parse_varint32(count))
					{
						return false;
					}
					for (int i=0; i<count; i++)
					{
						string s;
						if (!pbb->parse_string(s))
						{
							return false;
						}
						add_skill(s);
					}
					break;
				}

			default :
				{
					if (!pbb->parse_unknown_pass((DATA_TYPE)datatype))
					{
						return false;
					}
				}
				break;
			}
		}
		return true;
	}


public:
	int serialize(char* pOut, unsigned int len )
	{
		if (pOut == NULL)
		{
			return -1;
		}
		CBytebuffer bb(pOut, len, pFmt);
		return serialize(&bb);
	}
#define CHECK_RETURN(s)    if (!s)    {        return -1;    }

	int serialize(CBytebuffer* pbb)
	{
		if (pbb == NULL)
		{
			return -1;
		}

		if (has_age())
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_age, e_dt_with_head_length)));
			CHECK_RETURN(pbb->write_varint32(_age));
		}

		if (has_name())
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_name, e_dt_with_head_length)));
			CHECK_RETURN(pbb->write_string(_name));
		}

		if (size_of_skill()>0)
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_skill, e_dt_with_head_length)));
			char temp[1024] = "";
			CBytebuffer bb(temp, 1024, pFmt);
			for (unsigned int i = 0; i<size_of_skill(); i++)
			{
				CHECK_RETURN(bb.write_string(skill(i)));
			}
			CHECK_RETURN(pbb->write_varint32(bb.payload()));
			CHECK_RETURN(pbb->write_varint32(size_of_skill()));
			CHECK_RETURN(pbb->append(temp, bb.payload()));
		}

		return pbb->payload();
	}


};


#pragma once
#include "miniprotobuf_hlp.h"

class CMiniPb_student
{
public:


	CMiniPb_student(void)
	{

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));

	}


	CMiniPb_student(CMiniPb_student& r)
	{
		CMiniPb_student * pr = const_cast<CMiniPb_student *>(&r);

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));


		if (pr->has_school())
		{
			set_school(pr->school());
		}

		if (pr->has_who())
		{
			set_who(pr->who());
		}

		if (pr->has_classc())
		{
			set_classc(pr->classc());
		}

	}


	CMiniPb_student& operator =(CMiniPb_student& r)
	{
		CMiniPb_student * pr = const_cast<CMiniPb_student *>(&r);

		pFmt = new CEasyFormat();
		memset(&_mask, 0, sizeof(_mask));


		if (pr->has_school())
		{
			set_school(pr->school());
		}

		if (pr->has_who())
		{
			set_who(pr->who());
		}

		if (pr->has_classc())
		{
			set_classc(pr->classc());
		}

		return *this;
	}


	~CMiniPb_student(void)
	{

		if (pFmt != NULL)
		{
			delete pFmt;
		}

	}


public:
	//format
	CFormat* pFmt;
	//idx
	//mask
#define MASK_SIZE 1
	unsigned int _mask[MASK_SIZE];
	SET_BIT_HELP_EASY(_mask,MASK_SIZE);
	HAS_BIT_HELP_EASY(_mask,MASK_SIZE);

	//attr enum

	enum {

		e_school = 3,

		e_who = 2,

		e_classc = 1,

	};

	//attr

	OPTION_ATTR(string, school);

	OPTION_ATTR(CMiniPb_person, who);

	OPTION_ATTR(int, classc);



	bool parse(const char* pIn, unsigned int packetlen)
	{
		if (pIn == NULL || packetlen == 0)
		{
			return false;
		}
		CBytebuffer bb((char*)pIn, packetlen, pFmt);
		return parse(&bb);
	}

	bool parse(CBytebuffer* pbb)
	{
		if (pbb == NULL)
		{
			return false;
		}
		int tag = 0;
		int datatype = 0;
		while ((tag = pbb->get_tag_datatype(datatype))>0)
		{
			switch(tag)
			{

			case e_school:
				{
					if ( !pbb->parse_string(_school))
					{
						return false;
					}
					_set_bit(tag);
				}
				break;

			case e_classc:
				{
					if ( !pbb->parse_varint32(_classc))
					{
						return false;
					}
					_set_bit(tag);
				}
				break;

			case e_who:
				{
					int len = 0;
					if ( !pbb->parse_varint32(len))
					{
						return false;
					}
					CBytebuffer tb(pbb->get_cur(), len, pFmt);
					if ( !_who.parse(&tb))
					{
						return false;
					}
					pbb->advance(len);
					_set_bit(tag);
				}
				break;

			default :
				{
					if (!pbb->parse_unknown_pass((DATA_TYPE)datatype))
					{
						return false;
					}
				}
				break;
			}
		}
		return true;
	}


public:
	int serialize(char* pOut, unsigned int len )
	{
		if (pOut == NULL)
		{
			return -1;
		}
		CBytebuffer bb(pOut, len, pFmt);
		return serialize(&bb);
	}
#define CHECK_RETURN(s)    if (!s)    {        return -1;    }

	int serialize(CBytebuffer* pbb)
	{
		if (pbb == NULL)
		{
			return -1;
		}

		if (has_school())
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_school, e_dt_with_head_length)));
			CHECK_RETURN(pbb->write_string(_school));
		}

		if (has_classc())
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_classc, e_dt_with_head_length)));
			CHECK_RETURN(pbb->write_varint32(_classc));
		}

		if (has_who())
		{
			CHECK_RETURN(pbb->write_varint32(pbb->build_tag(e_who, e_dt_with_head_length)));
			char temp[1024];
			int len = 0;
			CHECK_RETURN((len = _who.serialize(temp, 1024))>0);
			CHECK_RETURN(pbb->write_varint32(len));
			CHECK_RETURN(pbb->append(temp, len));
		}

		return pbb->payload();
	}


};

